package com.lxq.live.controller;

import com.google.gson.Gson;
import com.lxq.live.dao.WordDao;
import com.lxq.live.pojo.WordPOJO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
public class SaveWordController {
    @Autowired
    WordDao wordDao;

    @GetMapping("/save_word")
    public String saveWord() throws IOException {
        System.out.println("整理单词中。。。。");
        var root="G:/临时文件/data_1638689021/words";
        var files=new File(root).listFiles();
        assert files != null;
        for (File file:files){
            BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream(file));
            var bytes=new byte[2048];
            var stringBuilder=new StringBuilder();
            while(inputStream.read(bytes)!=-1){
                stringBuilder.append(new String(bytes, StandardCharsets.UTF_8));
            }
            Gson gson=new Gson();
            WordPOJO wordPOJO=gson.fromJson(stringBuilder.toString().trim(),WordPOJO.class);
            //System.out.println(wordPOJO.toString());
            wordDao.save(wordPOJO);
        }
        return "整理完成";
    }
}
